//
//  Login2ViewController.swift
//  ddx0513
//
//  Created by ejian  on 15/5/29.
//  Copyright (c) 2015年 jiang yongbin. All rights reserved.
//

import UIKit

class Login2ViewController: UIViewController {
    var cellphone: String = ""
    var hud: MBProgressHUD?
    var password: String = ""
    var sourceNavi = true

    @IBOutlet weak var passwordTextField: UITextField!
    @IBOutlet weak var okButton: UIButton!
    @IBOutlet weak var forgetPwdLabel: UILabel!
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        self.navigationController?.setNavigationBarHidden(false, animated: true)
        self.okButton.enabled = false
        self.passwordTextField.addTarget(self, action: Selector("textFiledDidChange:"), forControlEvents: UIControlEvents.EditingChanged)
        
        var forgetPwdTapGesture = UITapGestureRecognizer(target: self, action: Selector("forgetPwdLabelTapped:"))
        forgetPwdLabel.addGestureRecognizer(forgetPwdTapGesture)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    func setNavi() {
        if (sourceNavi) {
            self.navigationController?.setNavigationBarHidden(true, animated: true)
        } else {
            self.navigationController?.setNavigationBarHidden(false, animated: true)
        }
    }
    
    @IBAction func okButtonPressed(sender: AnyObject) {
        startIndicator()
        password = self.passwordTextField.text
        var a = ["mobile": cellphone, "pwd": password]
        NetUtils.netRequest(Method.POST, URLString: NetUtils.getURLLogin(), parameters: a, view: self.view, responseHandler: stopIndicator, successHandler: login)
    }
    
    @IBAction func backButtonPressed(sender: AnyObject) {
        self.navigationController?.popViewControllerAnimated(true)
        setNavi()
    }
    
    func forgetPwdLabelTapped(sender: UILabel) -> Void {
        var storyBoard = UIStoryboard(name: "Login", bundle: nil)
        var viewController = storyBoard.instantiateViewControllerWithIdentifier("SettingPwd") as! SettingPwdViewController
        viewController.cellphone = self.cellphone
        viewController.sourceNavi = self.sourceNavi
        self.navigationController?.pushViewController(viewController, animated: true)
    }
    
    func textFiledDidChange(sender: UITextField) -> Void {
        if (sender == self.passwordTextField) {
            var str = self.passwordTextField.text
            var length = count(str)
            
            if (length == 0) {
                self.okButton.enabled = false
            } else {
                self.okButton.enabled = true
            }
        }
    }
    
    //MARK: - 网络回调函数
    
    func startIndicator() {
        hud = MBProgressHUD(view: self.view)
        self.view.addSubview(hud!)
        hud?.labelText = "登录中.."
        hud?.show(true)
    }
    
    func stopIndicator() {
        hud?.hide(false)
    }
    
    func login (json: JSON) {
        var userId = json["userid"].int
        var nickName = json["nickname"].string
        var imgUrl = json["imgurl"].string
        var pushAlias = json["push_alias"].string
        var pushTag = json["push_tag"].string
        var imUsername = json["im_username"].string
        var imPassword = json["im_password"].string
        var vipType = json["vip_type"].string
        var vipBegin = json["vip_begin"].int64
        var vipExpire = json["vip_expire"].int64
        var token = json["token"].string
        
        var userDefault = NSUserDefaults.standardUserDefaults()
        userDefault.setObject(cellphone, forKey: DBUtils.varNames.loginName)
        userDefault.setObject(password, forKey: DBUtils.varNames.password)
        userDefault.setObject(true, forKey: DBUtils.varNames.loginStatus)
        userDefault.synchronize()
        
        DBUtils.mem.loginStatus = true
        DBUtils.mem.loginName = cellphone
        if let a = userId {
            DBUtils.mem.userId = a
        }
        if let a = nickName {
            DBUtils.mem.nickName = a
        }
        if let a = imgUrl {
            DBUtils.mem.imgURL = a
        }
        if let a = pushAlias {
            DBUtils.mem.pushAlias = a
        }
        if let a = pushTag {
            DBUtils.mem.pushTag = a
        }
        if let a = imUsername {
            DBUtils.mem.imUsername = a
        }
        if let a = imPassword {
            DBUtils.mem.imPassword = a
        }
        if let a = token {
            DBUtils.mem.token = a
        }
        if let a = vipType {
            DBUtils.mem.vipType = a
        }
        if let a = vipBegin {
            DBUtils.mem.vipBegin = a
        }
        if let a = vipExpire {
            DBUtils.mem.vipExpire = a
        }
        
        rootController.navigationController?.popToRootViewControllerAnimated(true)
        rootController.navigationController?.setNavigationBarHidden(true, animated: true)
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
